package io.xxx.walle.room;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.xxx.walle.room.api.JoinRoomRequest;
import io.xxx.walle.room.api.LeaveRoomRequest;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class RoomService {

    @Resource
    private RoomRepository roomRepository;

    public void joinRoom(JoinRoomRequest request) {
        LocalDateTime now = LocalDateTime.now();

        Room room = request.getRoom();
        room.setId(IdWorker.getId());
        room.setJoinedTime(request.getDate());
        room.setCreatedTime(now);
        room.setUpdatedTime(now);

        roomRepository.save(request.getRoom());
    }

    public void leaveRoom(LeaveRoomRequest request) {
        Room room = request.getRoom();
        Long roomId = room.getId();
        roomRepository.deleteById(roomId);
    }
}
